/*
 * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.wso2.carbon.registry.custom.skb.ui.utils;

import org.wso2.carbon.registry.custom.skb.ui.beans.KBItem;
import org.wso2.carbon.registry.common.ui.UIException;
import org.wso2.carbon.registry.resource.ui.clients.CustomUIServiceClient;
import org.apache.axiom.om.util.AXIOMUtil;
import org.apache.axiom.om.OMElement;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpSession;
import javax.xml.namespace.QName;

public class GetKBUtil {

    private static final Log log = LogFactory.getLog(GetKBUtil.class);

    public static KBItem getKBItem(
            String path, ServletConfig config, HttpSession session) throws UIException {
//TODO change onclick submission of form to action in form tag
        try {
            CustomUIServiceClient customUIServiceClient =
                    new CustomUIServiceClient(config, session);

            String content = customUIServiceClient.getTextContent(path);
            OMElement problem = AXIOMUtil.stringToOM(content);

            String problemName = problem.getAttributeValue(new QName(null, "problemName"));
            String description = problem.getAttributeValue(new QName(null, "description"));
            String link = problem.getAttributeValue(new QName(null, "link"));

            OMElement questions = problem.getFirstChildWithName(new QName(null, "questions"));
            String question = questions.getAttributeValue(new QName(null, "question"));

            OMElement answers = problem.getFirstChildWithName(new QName(null, "answers"));
            String answer = answers.getAttributeValue(new QName(null, "answer"));

            OMElement solutions = problem.getFirstChildWithName(new QName(null, "solutions"));
            String solution = solutions.getAttributeValue(new QName(null, "solution"));

            OMElement keyWords = problem.getFirstChildWithName(new QName(null, "keyWords"));
            String keyWord = keyWords.getAttributeValue(new QName(null, "keyWord"));

            OMElement products = problem.getFirstChildWithName(new QName(null, "products"));
            String product = products.getAttributeValue(new QName(null, "product"));
            String productRelease = products.getAttributeValue(new QName(null, "productRelease"));

            OMElement platform = problem.getFirstChildWithName(new QName(null, "platform"));
            String carbonPlatform = platform.getAttributeValue(new QName(null, "carbonPlatform"));
            String jdkPlatform = platform.getAttributeValue(new QName(null, "jdkPlatform"));


            KBItem kbItem = new KBItem();
            kbItem.setProblemName(problemName);
            kbItem.setDescription(description);
            kbItem.setLink(link);
            kbItem.setQuestion(question);
            kbItem.setAnswer(answer);
            kbItem.setSolution(solution);
            kbItem.setKeyWord(keyWord);
            kbItem.setProduct(product);
            kbItem.setProductRelease(productRelease);
            kbItem.setCarbonPlatform(carbonPlatform);
            kbItem.setJDKPlatform(jdkPlatform);

            return kbItem;

        } catch (Exception e) {
            String msg = "Failed to get KB B Item details. " + e.getMessage();
            log.error(msg, e);
            throw new UIException(msg, e);
        }
    }
}
